package com.dabao.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

/**
 * Created by card on 16/7/11.
 */
@Service
public class RedisService
{

    @Autowired
    private JedisPool jedisPool;

    public void set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        try  {
            jedis.set(key, value);
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
    }
    
    public String get(String key) {
        Jedis jedis = jedisPool.getResource();
        try  {
            return jedis.get(key);
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 向队列中放入值
     * @param key
     * @param value
     */
    public void rpush(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        try  {
            jedis.rpush(key, value);
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
    }

    public Long llen(String key) {
        Jedis jedis = jedisPool.getResource();
        try  {
            return jedis.llen(key);
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 非阻塞的弹出队列的头
     * @param key
     * @return
     */
    public String lpop(String key) {
        Jedis jedis = jedisPool.getResource();
        try  {
            return jedis.lpop(key);
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 阻塞的弹出队列的头
     * @param key
     * @return
     */
    public String blpop(String key) {
        Jedis jedis = jedisPool.getResource();
        try  {
            List<String> list = jedis.blpop(1, key);
            if(list != null && list.size() >= 2) {
                return list.get(1);
            }
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
        return null;
    }


    /**
     * 使一个key过期
     * @param key
     * @param seconds
     */
    public void expire(String key, int seconds) {
        Jedis jedis = jedisPool.getResource();
        try  {
            jedis.expire(key, seconds);
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
    }

    public boolean lock(String key) {
        Jedis jedis = jedisPool.getResource();
        try  {
            return jedis.hsetnx("mobile_lock", key, "1") == 1 ? true : false;
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
    }

    public boolean unlock(String key) {
        Jedis jedis = jedisPool.getResource();
        try  {
            return jedis.hdel("mobile_lock", key) == 1 ? true : false;
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
    }

    public void hset(String key, String field, String value) {
        Jedis jedis = jedisPool.getResource();
        try  {
            jedis.hset(key, field, value);
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
    }

    public String hget(String key, String field) {
        Jedis jedis = jedisPool.getResource();
        try  {
            return jedis.hget(key, field);
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
    }

    public void hdel(String key, String field) {
        Jedis jedis = jedisPool.getResource();
        try  {
            jedis.hdel(key, field);
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
    }

    public Map<String, String> hgetAll(String key) {
        Jedis jedis = jedisPool.getResource();
        try  {
            return jedis.hgetAll(key);
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
    }
}
